/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-09-25
 */
package org.jeecg.modules.flow.callback.delivery.in;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.delivery.util.CommonDeliveryTaskUtil;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.enums.SimpleDocumentTypeEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 入库任务移库回调前bean （暂时不用）
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-09-25
 */
@RequiredArgsConstructor
@Component
public class R101InBoundTaskDBRKMoveBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsInReceiptTaskService inReceiptTaskService;

    private final IWmsInReceiptTaskMaterialService inReceiptTaskMaterialService;

    private final CommonDeliveryTaskUtil commonDeliveryTaskUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        // 判断是否全部移库
        List<WmsInReceiptTaskMaterial> inReceiptTaskMaterialList = inReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskId)
        );
        //是否都已经移库
        boolean allMatch = inReceiptTaskMaterialList.stream().allMatch(i -> {
            Integer hasMoved = Objects.isNull(i.getHasMoved()) ? 0 : i.getHasMoved();
            return hasMoved.equals(1);
        });
        if(allMatch){
            //推动节点
            baseFlowDto.setRunThenStop(false);
            commonDeliveryTaskUtil.updateMaterialDeliveryStatus(SimpleDocumentTypeEnum.IN_TASK,taskId,null,"已完成",null);
            inReceiptTaskService.update(new LambdaUpdateWrapper<WmsInReceiptTask>()
                    .set(WmsInReceiptTask::getDocumentStatus, DocumentStatusEnum.COMPLETE.getCode())
                    .eq(WmsInReceiptTask::getId,taskId)
            );
            commonDeliveryTaskUtil.pushInFlow(taskId);
        }else{
            // 不推动节点
            baseFlowDto.setRunThenStop(true);

        }
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(taskId);
        baseFlowDto.setResult(Result.OK());
    }
}
